﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using NPOI.OpenXmlFormats.Spreadsheet;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class RepairApplicationService : BaseService, IRepairApplicationService
    {
        private readonly IRepairApplicationRepository _repairApplicationRepository;
        private readonly IUnitOfWork _unitOfWork;
        public RepairApplicationService(IMapper mapper, IRepairApplicationRepository _repairApplicationRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._repairApplicationRepository = _repairApplicationRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddRepairApplication(RepairApplication repairApplication)
        {
            _repairApplicationRepository.AddEntity(repairApplication);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelRepairApplication(long id)
        {
            var cust = _repairApplicationRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _repairApplicationRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<RepairApplication> GetRepairApplication(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _repairApplicationRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                custList = custList.Where(c => c.Name.Contains(name));
            }
            var list = _repairApplicationRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public List<RepairApplication> GetRepairApplicationMsgDown()
        {
            var list = _repairApplicationRepository.GetAllEntityList();
            return list;
        }

        public RepairApplication? GetRepairApplicationById(long id)
        {
            return _repairApplicationRepository.QueryEntityById(id);
        }

        public bool UpdRepairApplication(RepairApplication RepairApplication)
        {
            var aobj = _repairApplicationRepository.QueryIsEntityWhere(x => x.Id == RepairApplication.Id);
            if (aobj == null)
            {
                return false;
            }
            _repairApplicationRepository.UpdEntity(RepairApplication);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
